package app.server.project.core.token;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Component;

@Component
public class TokenCache {


    private final static String TokenKey = "150249579f33cb0b9cb2fafb8d9d61e8";
    private static final TimedCache<Long, String> map = CacheUtil.newTimedCache(1000 * 60 * 60);
    private static boolean debug;

    public static boolean chkToken(long userId, String token) {
        if (debug) return true;
        if (StrUtil.isBlank(token)) return false;
        var cache = map.get(userId);
        return token.equals(cache);
    }

    public static void setDebug(boolean flag) {
        debug = flag;
    }

    public static void kick(long userId) {
        map.remove(userId);
    }

    /**
     * 加入缓存
     *
     * @param userId
     * @param
     * @return
     */
    public static String addToCache(long userId) {
        String token = IdUtil.simpleUUID();
        map.put(userId, token);
        return token;
    }

}
